/*
 * Copyright © 2019-2020  Whale Cloud, Inc. All Rights Reserved.
 *
 * Notice: Whale Cloud Inc copyrights this specification.
 * No part of this specification may be reproduced in any form or means,
 * without the prior written consent of Whale Cloud Inc.
 *
 */
package com.wteam.modules.system.web;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.wteam.annotation.permission.PermissionGroup;
import com.wteam.annotation.rest.AnonymousGetMapping;
import com.wteam.domain.vo.R;
import com.wteam.modules.system.domain.SelectCourseRecord;
import com.wteam.modules.system.service.SelectCourseRecordService;
import com.wteam.modules.system.domain.criteria.SelectCourseRecordQueryCriteria;
import com.wteam.modules.system.util.MqUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * 选课记录 控制层.
 *
 * @author herry_wong
 * @since 2020-12-29
 */
@SuppressWarnings({"rawtypes"})
@Api(value = "选课记录Controller", tags = {"新增：选课记录操作"})
@RestController
@RequiredArgsConstructor
@RequestMapping("api/selectCourseRecord")
@PermissionGroup(value = "SELECTCOURSERECORD", aliasPrefix = "选课记录")
public class SelectCourseRecordController {

    private final SelectCourseRecordService selectCourseRecordService;

    private final MqUtil mqUtil;

    //@Log("查询选课记录分页")
    @ApiOperation(value = "查询选课记录分页")
    @GetMapping(value = "/page")
    @PreAuthorize("@R.check('SELECTCOURSERECORD:all', 'SELECTCOURSERECORD:list')")
    public R getSelectCourseRecords(SelectCourseRecordQueryCriteria criteria, Pageable pageable){
        return R.ok(selectCourseRecordService.queryAll(criteria, pageable));
    }

    //@Log("查询选课记录详情")
    @ApiOperation(value = "查询选课记录详情")
    @GetMapping(value = "/get/{id}")
    @PreAuthorize("@R.check('SELECTCOURSERECORD:all', 'SELECTCOURSERECORD:list')")
    public R get(@PathVariable Long id){
        return R.ok(selectCourseRecordService.findDTOById(id));
    }

    //@Log("新增选课记录")
    @ApiOperation(value = "新增选课记录")
    @PostMapping(value = "/add")
    @PreAuthorize("@R.check('SELECTCOURSERECORD:all', 'SELECTCOURSERECORD:add')")
    public R create(@Validated @RequestBody SelectCourseRecord resources){
        return R.ok(selectCourseRecordService.create(resources));
    }

    //@Log("修改选课记录")
    @ApiOperation(value = "修改选课记录")
    @PostMapping(value = "/edit")
    @PreAuthorize("@R.check('SELECTCOURSERECORD:all', 'SELECTCOURSERECORD:edit')")
    public R update(@Validated(SelectCourseRecord.Update.class) @RequestBody SelectCourseRecord resources){
        selectCourseRecordService.update(resources);
        return R.ok();
    }

    //@Log("删除选课记录")
    @ApiOperation(value = "删除选课记录")
    @PostMapping(value = "/del")
    @PreAuthorize("@R.check('SELECTCOURSERECORD:all', 'SELECTCOURSERECORD:del')")
    public R delete(@RequestBody Set<Long> ids){
        selectCourseRecordService.deleteAll(ids);
        return R.ok();
    }

    //@Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@R.check('SELECTCOURSERECORD:all', 'SELECTCOURSERECORD:list')")
    public void download(HttpServletResponse response, SelectCourseRecordQueryCriteria criteria) throws IOException {
        selectCourseRecordService.download(selectCourseRecordService.queryAll(criteria), response);
    }

    @ApiOperation("send")
    @AnonymousGetMapping(value = "/send")
    public void send(Long id, Long studentId, Long courseId){
        SelectCourseRecord selectCourseRecord = new SelectCourseRecord();
        selectCourseRecord.setId(id);
        selectCourseRecord.setCourseId(courseId);
        selectCourseRecord.setStudentId(studentId);
         String result = mqUtil.sendToSelectCourseQueue1(selectCourseRecord);
        System.out.println(result);
    }
}